#include<stdio.h>
#include<stdlib.h>
#include "../inc/LinkStack.h"

#if StackDataType == CHAR
    #define STACK_FORMAT "%c"
#else
    #define STACK_FORMAT "%d"
#endif

LINKSTACK *InitStack()
{
    LINKSTACK *l=malloc(sizeof(LINKSTACK));
    l->count=0;
    l->top=NULL;
    l->bottom=NULL;
    return l;
}
//入栈
void push(LINKSTACK *l,StackDataType data)
{
    if(l==NULL)
    {
        printf("stack is null!");
        return;
    }
    if(l->top==NULL)
    {
        l->top=malloc(sizeof(NODE));
        l->bottom=l->top;
        l->count++;
        l->top->data=data;
        l->top->next=NULL;
        l->top->prev=NULL;
    }
    else
    {
        l->top->next=malloc(sizeof(NODE));
        l->top->next->data=data;
        l->top->next->next=NULL;
        l->top->next->prev=l->top;
        l->top=l->top->next;
        l->count++;
    }
}
//出栈
StackDataType pop(LINKSTACK *l)
{
    if(l==NULL||l->top==NULL)
    {
        printf("stack is null!\n");
        return 0;
    }
    StackDataType pop_data=l->top->data;
    if(l->top->prev==NULL)
    {
        free(l->top);
        l->top=NULL;
        l->bottom=NULL;
        l->count--;
    }
    else
    {
        l->top=l->top->prev;
        free(l->top->next);
        l->top->next=NULL;
        l->count--;
    }
    return pop_data;
}
//获取top
StackDataType GetTop(LINKSTACK *l)
{
    if(l==NULL||l->top==NULL)
    {
        printf("stack is null!\n");
        return 0;
    }
    return l->top->data;
}
//数量
int StackLen(LINKSTACK *l)
{
    if(l==NULL||l->top==NULL)
    {
        return 0;
    }
    return l->count;
}
//销毁
void DestroyStack(LINKSTACK *list)
{
    if(list==NULL||list->top==NULL)
    {
        printf("stack is null!\n");
    }
    for(NODE *p=list->top;p!=NULL;)
    {
        if(p->prev==NULL)
        {
            free(p);
            p=NULL;
        }
        else
        {
            p=p->prev;

            free(p->next);
        }
    }
    list->bottom=NULL;
    list->top=NULL;
    list->count=0;
    free(list);
}
//清空
void ClearStack(LINKSTACK *l)
{
    if(l==NULL||l->top==NULL)
    {
        printf("stack is null!\n");
    }
    for(NODE *p=l->top;p!=NULL;)
    {
        if(p->prev==NULL)
        {
            free(p);
            p=NULL;
        }
        else
        {
            p=p->prev;
            free(p->next);
        }
    }
    l->bottom=NULL;
    l->top=NULL;
    l->count=0;
}
//打印
void PrintLinkStack(LINKSTACK *l)
{
    if(l==NULL)
    {
        printf("stack is null!\n");
    }
    else if(l->top==NULL)
    {
        printf("stack is empty!\n");
    }
    else
    {
        printf("bottom:" STACK_FORMAT "  top:" STACK_FORMAT "  count:%d  \n", 
               l->bottom->data, l->top->data, l->count);
        printf("print from bottom:");
        for(NODE *p=l->bottom;;p=p->next)
        {
            printf(STACK_FORMAT,p->data);
            if(p==l->top)
            {
                break;
            }
        }
        printf("\n");
        printf("print from top:");
        for(NODE *p=l->top;;p=p->prev)
        {
            printf("%c",p->data);
            if(p==l->bottom)
            {
                break;
            }
        }
        printf("\n");
    }
}